/* GUI CONVERTED from woworca.p (converter v1.76) Fri May  9 04:23:08 2003 */
/* woworca.p - WORK ORDER RECEIPT SUBROUTINE TO CREATE TRANSACTION HISTORY   */
/* Copyright 1986-2003 QAD Inc., Carpinteria, CA, USA.                       */
/* All rights reserved worldwide.  This is an unpublished work.              */
/* $Revision: 1.42 $                                      */
/* REVISION: 5.0      LAST MODIFIED: 06/15/89   BY: MLB *B130*               */
/* REVISION: 5.0      LAST MODIFIED: 06/23/89   BY: MLB *B159*               */
/* REVISION: 5.0      LAST MODIFIED: 07/05/89   BY: BJJ *B106*               */
/* REVISION: 5.0      LAST MODIFIED: 09/20/89   BY: emb *B265*               */
/* REVISION: 5.0      LAST MODIFIED: 01/23/90   BY: MLB *B522*               */
/* REVISION: 6.0      LAST MODIFIED: 04/23/90   BY: MLB *D002*               */
/* REVISION: 6.0      LAST MODIFIED: 04/23/90   BY: WUG *D002*               */
/* REVISION: 6.0      LAST MODIFIED: 03/18/91   BY: WUG *D472*               */
/* REVISION: 6.0      LAST MODIFIED: 06/19/91   BY: RAM *D717*               */
/* REVISION: 6.0      LAST MODIFIED: 10/03/91   BY: alb *D887*               */
/* REVISION: 7.0      LAST MODIFIED: 10/23/91   BY: pma *F003*               */
/* REVISION: 7.0      LAST MODIFIED: 01/28/92   BY: pma *F104*               */
/* REVISION: 7.0      LAST MODIFIED: 02/12/92   BY: pma *F190*               */
/* REVISION: 7.0      LAST MODIFIED: 02/21/92   BY: pma *F085*               */
/* REVISION: 7.0      LAST MODIFIED: 07/09/92   BY: pma *F748*               */
/* Revision: 7.3      Last edit: 09/27/92       By: jcd *G247*               */
/* Revision: 7.3      Last edit: 02/03/93       By: fwy *G630*               */
/* REVISION: 7.3      LAST MODIFIED: 03/25/93   BY: emb *G871*               */
/* REVISION: 7.2      LAST MODIFIED: 04/12/94   BY: pma *FN34*               */
/* REVISION: 7.2      LAST MODIFIED: 05/06/94   BY: ais *FO02*               */
/* REVISION: 7.3      LAST MODIFIED: 10/06/94   BY: pxd *FR90*               */
/* REVISION: 7.3      LAST MODIFIED: 10/11/94   BY: pxd *FS31*               */
/* REVISION: 8.5      LAST MODIFIED: 10/12/94   BY: TAF *J035*               */
/* REVISION: 8.5      LAST MODIFIED: 11/10/94   BY: TAF *J038*               */
/* REVISION: 8.5      LAST MODIFIED: 03/25/95   BY: pma *J040*               */
/* REVISION: 8.5      LAST MODIFIED: 03/26/95   BY: ktn *J046*               */
/* REVISION: 8.5      LAST MODIFIED: 04/26/95   BY: sxb *J04D*               */
/* REVISION: 7.3      LAST MODIFIED: 08/17/95   BY: qzl *F0TC*               */
/* REVISION: 8.5      LAST MODIFIED: 09/30/95   BY: sxb *J053*               */
/* REVISION: 7.3      LAST MODIFIED: 12/19/95   BY: rvw *G1GN*               */
/* REVISION: 8.5      LAST MODIFIED: 04/10/96   BY: *J04C* Sue Poland        */
/* REVISION: 8.6      LAST MODIFIED: 03/15/97   BY: *K04X* Steve Goeke       */
/* REVISION: 8.6      LAST MODIFIED: 10/23/97   BY: *J244* Felcy D'Souza     */
/* REVISION: 8.6E     LAST MODIFIED: 02/23/98   BY: *L007* A. Rahane         */
/* Old ECO marker removed, but no ECO header exists *F0PN*                   */
/* Old ECO marker removed, but no ECO header exists *K1Q4*                   */
/* REVISION: 8.6E     LAST MODIFIED: 06/29/98   BY: *L034* Markus Barone     */
/* REVISION: 8.6E     LAST MODIFIED: 08/14/98   BY: *J2W7* Santhosh Nair     */
/* REVISION: 8.6E     LAST MODIFIED: 02/09/99   BY: *J39M* G.Latha           */
/* REVISION: 8.6E     LAST MODIFIED: 03/25/99   BY: *J39K* Sanjeev Assudani  */
/* REVISION: 9.1      LAST MODIFIED: 10/01/99   BY: *N014* Vijaya Pakala     */
/* REVISION: 9.1      LAST MODIFIED: 10/25/99   BY: *N002* B. Gates          */
/* REVISION: 9.1      LAST MODIFIED: 08/12/00   BY: *N0KC* Mark Brown        */
/* REVISION: 9.1      LAST MODIFIED: 08/17/00   BY: *L0X7* Sathish Kumar     */
/* REVISION: 9.1      LAST MODIFIED: 09/27/00   BY: *L13S* Vandna Rohira     */
/* REVISION: 9.1      LAST MODIFIED: 10/16/00   BY: *N0SX* Jean Miller       */
/* REVISION: 9.1      LAST MODIFIED: 11/06/00   BY: *N0TN* Jean Miller       */
/* REVISION: 9.1      LAST MODIFIED: 08/02/00   BY: *N0W1* Mudit Mehta       */
/* Revision: 1.30     BY: Katie Hilbert  DATE: 04/01/01 ECO: *P008*   */
/* Revision: 1.30     BY: Vivek Gogte    DATE: 05/31/01 ECO: *M157*   */
/* Revision: 1.31     BY: Sathish Kumar  DATE: 06/04/01 ECO: *M199*   */
/* Revision: 1.32     BY: Niranjan R.    DATE: 07/23/01 ECO: *P00L*   */
/* Revision: 1.33     BY: Vivek Gogte    DATE: 08/04/01 ECO: *M19J*   */
/* Revision: 1.34     BY: Ellen Borden   DATE: 03/14/02 ECO: *P00G*   */
/* Revision: 1.35     BY: Jeff Wootton   DATE: 04/29/02 ECO: *P03G*   */
/* Revision: 1.37    BY: Niranjan R.  DATE: 06/25/02 ECO: *P09L*   */
/* Revision: 1.38     BY: Dorota Hohol     DATE: 02/25/03  ECO: *P0N6* */
/* Revision: 1.41     BY: Narathip W.      DATE: 04/19/03  ECO: *P0Q7* */
/* $Revision: 1.42 $    BY: Narathip W.      DATE: 05/09/03  ECO: *P0RN* */

/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/
/*V8:ConvertMode=Maintenance                                                  */

{mfdeclre.i}
{gplabel.i}
{cxcustom.i "WOWORCA.P"}
{&WOWORCA-P-TAG1}

/* ********** Begin Translatable Strings Definitions ********* */

&SCOPED-DEFINE woworca_p_3 "Close"
/* MaxLen: Comment: */

&SCOPED-DEFINE woworca_p_4 "Conversion"
/* MaxLen: Comment: */

/* ********** End Translatable Strings Definitions ********* */

define input parameter pTrLot      like tr_lot    no-undo.
define input parameter p_accum_wip like tr_gl_amt no-undo.

define shared variable rmks like tr_rmks.
define shared variable serial like tr_serial.
define shared variable eff_date like glt_effdate.
define shared variable conv like um_conv label {&woworca_p_4} no-undo.
define shared variable reject_conv like conv no-undo.
define shared variable ref like glt_ref.
define shared variable wo_recno as recid.
define shared variable pl_recno as recid.
define shared variable cline as character.
define shared variable close_wo like mfc_logical label {&woworca_p_3}.

define new shared variable loc like ld_loc.
define new shared variable transtype as character format "x(7)".
define new shared variable wo_recid as recid.
define new shared variable sod_recno as recid.
define new shared variable sct_recno as recid.
define new shared variable sf_cr_acct like dpt_lbr_acct.
define new shared variable sf_dr_acct like dpt_lbr_acct.
define new shared variable sf_cr_sub like dpt_lbr_sub.
define new shared variable sf_dr_sub like dpt_lbr_sub.
define new shared variable sf_cr_cc like dpt_lbr_cc.
define new shared variable sf_dr_cc like dpt_lbr_cc.
define new shared variable sf_gl_amt like tr_gl_amt.
define new shared variable sf_entity like en_entity.
define new shared variable newcst as decimal.
define new shared variable tr_recno as recid.

define variable i as integer.
define variable gl_amt like glt_amt.
define variable glcost like sct_cst_tot.
define variable qty_left like tr_qty_chg.
define variable gl_cost like sct_cst_tot.
define variable assay like tr_assay.
define variable grade like tr_grade.
define variable expire like tr_expire.
define variable newmtl_tl as decimal.
define variable newlbr_tl as decimal.
define variable newbdn_tl as decimal.
define variable newovh_tl as decimal.
define variable newsub_tl as decimal.
define variable newmtl_ll as decimal.
define variable newlbr_ll as decimal.
define variable newbdn_ll as decimal.
define variable newovh_ll as decimal.
define variable newsub_ll as decimal.
define variable glx_mthd like cs_method.
define variable glx_set like cs_set.
define variable cur_mthd like cs_method.
define variable cur_set like cs_set.
define variable reavg_yn as logical.
define variable qty_chg like tr_qty_loc.
define variable msgref like tr_msg.
define variable open_ref like mrp_qty.
define variable trans-ok like mfc_logical.
define variable qad_wkfl_id     as   character.
define variable gl_tmp_amt as decimal no-undo.
define variable dftAcct       like pl_pur_acct no-undo.
define variable dftSubAcct    like pl_pur_sub  no-undo.
define variable dftCostCenter like pl_pur_cc   no-undo.
define variable iss_trnbr like tr_trnbr no-undo.
define variable rct_trnbr like tr_trnbr no-undo.

define variable l_wo_nbr like wo_nbr no-undo.
define variable l_wo_lot like wo_lot no-undo.
{&WOWORCA-P-TAG5}

/* THESE SHARED VARIABLES ARE FOR ICINTR1A.P */
define new shared variable pt_recid    as   recid.
define new shared variable cr_acct     like trgl_cr_acct.
define new shared variable cr_sub      like trgl_cr_sub.
define new shared variable cr_cc       like trgl_cr_cc.
define new shared variable qty_iss_rcv like tr_qty_loc.
define new shared variable accum_wip   like trgl_gl_amt.
define new shared variable cr_proj     like trgl_cr_proj.
define new shared variable wip_entity  like si_entity.
define new shared variable sct_recid   as   recid.

{&WOWORCA-P-TAG2}

/* DEFINE VARIABLES FOR CHANGE ATTRIBUTES FEATURE */
{gpatrdef.i "shared"}
find first gl_ctrl no-lock.
find first icc_ctrl no-lock no-error.

/* PROCEDURE FOR ROUNDING FOR GIVEN ROUNDING METHOD */
{gpcrnd.i}

find wo_mstr where recid(wo_mstr) = wo_recno no-lock no-error.
if not available wo_mstr then leave.

if wo_joint_type <> "" and wo_joint_type <> "5"
then cline = "RCT" + wo_part.
else cline = "".

/* VARIABLES l_wo_nbr AND l_wo_lot ARE USED WHILE CREATING TR_HIST.           */
/* IF IT IS A FLOW ORDER THEN TR_LOT WILL HAVE NEW SEQUENCE NUMBER pTrLot AND */
/* TR_NBR WILL BE BLANK. FOR ALL OTHER TYPES OF WORK ORDERS TR_LOT WILL HAVE  */
/* WO_LOT AND TR_NBR  WILL HAVE WO_NBR */

if pTrLot <> " " and wo_type = "W" then
   assign
      l_wo_nbr = ""
      l_wo_lot = pTrLot.
else
   assign
      l_wo_nbr = wo_nbr
      l_wo_lot = wo_lot.

/* p_accum_wip WILL HAVE GL_AMT OF ALL ISS-WO TRANSACTIONS OF COMPONENTS */
/* accum_wip WILL BE USED IN icintr1a.p TO CALCULATE VARIANCES           */
accum_wip = p_accum_wip.

/* DETERMINE COSTING METHOD */
{gprun.i ""csavg01.p"" "(input wo_part,
                         input wo_site,
                         output glx_set,
                         output glx_mthd,
                         output cur_set,
                         output cur_mthd)"
}
/*GUI*/ if global-beam-me-up then undo, leave.


find wo_mstr no-lock where recid(wo_mstr) = wo_recno.
find pt_mstr no-lock where pt_part = wo_part.
find pl_mstr where pl_prod_line = pt_prod_line no-lock.
qad_wkfl_id = mfguser + "wojprc.p".
find first qad_wkfl
   where qad_wkfl.qad_key1 = qad_wkfl_id
   and   qad_wkfl.qad_key2 = string(wo_part,"x(18)")
                           + string(wo_lot,"x(8)") no-error.
if available qad_wkfl then do:
   assign
      chg_assay = qad_wkfl.qad_decfld[13]
      chg_grade = qad_wkfl.qad_charfld[11]
      chg_expire = qad_wkfl.qad_datefld[4]
      chg_status = qad_wkfl.qad_charfld[12]
      assay_actv = yes
      grade_actv = yes
      expire_actv = yes.
   if qad_wkfl.qad_charfld[15] = "Y" then
      status_actv  = yes.
   else
      status_actv = no.
end.

{gprun.i ""glactdft.p"" "(input ""WO_WIP_ACCT"",
                          input pt_prod_line,
                          input wo_site,
                          input """",
                          input """",
                          input no,
                          output dftAcct,
                          output dftSubAcct,
                          output dftCostCenter)"}
/*GUI*/ if global-beam-me-up then undo, leave.

assign
   cr_acct   = if wo_acct <> "" then wo_acct else dftAcct
   cr_sub    = if wo_acct <> "" then wo_sub  else dftSubAcct
   cr_cc     = if wo_acct <> "" then wo_cc   else dftCostCenter
   transtype = "RCT-WO".

/* UPDATE CURRENT COST & POST ANY GL DISCREPANCY */
/* CALCULATE AMOUNTS TO AVERAGE BY COST CATEGORY */
if glx_mthd = "AVG" or cur_mthd = "AVG" or cur_mthd = "LAST"
then do transaction:
/*GUI*/ if global-beam-me-up then undo, leave.


   qadloop:
   repeat on error undo qadloop, retry:
      find qad_wkfl
         where qad_key1 = "wo_status" and qad_key2 = wo_lot
         exclusive-lock no-wait no-error.

      if locked qad_wkfl then do:
         hide message no-pause.
         pause 5 no-message.
         undo qadloop, retry.
      end.

      leave qadloop.
   end.

   if available qad_wkfl then delete qad_wkfl.

   if close_wo then do:
      create qad_wkfl.
      assign
         qad_key1 = "wo_status"
         qad_key2 = wo_lot.
      if recid(qad_wkfl) = -1 then .
   end.

   qty_chg = wo_qty_chg + wo_rjct_chg.

   if wo_type <> "R" and wo_type <> "E" then do:
      {gprun.i ""csavg02a.p"" "(input wo_part,
                               input wo_site,
                               input transtype,
                               input 0,
                               input recid(wo_mstr),
                               input wo_nbr,
                               input qty_chg,
                               input 0,
                               input glx_set,
                               input glx_mthd,
                               input cur_set,
                               input cur_mthd,
                               output newmtl_tl,
                               output newlbr_tl,
                               output newbdn_tl,
                               output newovh_tl,
                               output newsub_tl,
                               output newmtl_ll,
                               output newlbr_ll,
                               output newbdn_ll,
                               output newovh_ll,
                               output newsub_ll,
                               output newcst,
                               output reavg_yn,
                               output msgref)"
         }
/*GUI*/ if global-beam-me-up then undo, leave.

   end.

   if available qad_wkfl then delete qad_wkfl.
end.
/*GUI*/ if global-beam-me-up then undo, leave.


/* IN JOINT PRODUCTS RECEIPTS, INVENTORY IS RECEIVED AT THE EXISTING */
/* COSTS. RE-AVERAGING IS DONE ONLY AT THE TIME OF ACCOUNTING CLOSE. */
/* wo_xxx_totx FIELDS ARE UPDATED TO STORE THE AMOUNT MOVED TO       */
/* INVENTORY AT WORK ORDER RECEIPT WHICH ARE USED BY ACCOUNTING CLOSE*/
/* TO KNOW HOW MUCH COST WAS MOVED TO INVENTORY AT WORK ORDER RECEIPT*/
/* DURING ACCOUNTING CLOSE BASE PROCESS COST IS ALLOCATED TO THE     */
/* JOINT PRODUCTS BASED ON ALLOCATION METHOD AND THE DIFFERENCE      */
/* BETWEEN WORK ORDER RECEIPT COST AND GL COST IS MOVED BACK TO      */
/* INVENTORY                                                         */

if wo_joint_type <> ""
     and wo_type <> "R"
     and wo_type <> "E"
then do transaction:

   find wo_mstr where recid(wo_mstr) = wo_recno
   exclusive-lock.
   qty_chg = wo_qty_chg + wo_rjct_chg.
   run update_wo_mstr (input glx_set,
                       input wo_part,
                       input wo_site,
                       input qty_chg,
                       input glx_mthd,
                       input wo_joint_type,
                       input-output wo_mtl_totx,
                       input-output wo_lbr_totx,
                       input-output wo_bdn_totx,
                       input-output wo_sub_totx).

   if cur_mthd = "AVG"
   then do:
      run update_wo_mstr (input cur_set,
                          input wo_part,
                          input wo_site,
                          input qty_chg,
                          input cur_mthd,
                          input wo_joint_type,
                          input-output wo_mtl_tot,
                          input-output wo_lbr_tot,
                          input-output wo_bdn_tot,
                          input-output wo_sub_tot).
   end. /* IF cur_mthd = "AVG" */

end. /* IF wo_joint_type <> "" ... */

if wo_qty_chg <> 0
   or can-find (first sr_wkfl where sr_userid = mfguser)
then do:

   /***************************************************************/
   /*   reavg_yn    wo_qty_ord      Receipt        Scrap          */
   /*   ========    ==========      ===========    ===========    */
   /*   Yes          > 0            newcst         newcst         */
   /*   Yes          < 0  ILLEGAL   -------        -------        */
   /*   No           > 0            sct_cst_tot    newcst         */
   /*   No           < 0            sct_cst_tot    sct_cst_tot    */
   /***************************************************************/

   do transaction:
/*GUI*/ if global-beam-me-up then undo, leave.


      find sct_det where sct_part = pt_part
         and sct_site = wo_site
         and sct_sim  = glx_set
         no-lock no-error.
      if not available sct_det then
      {gpsct01.i &set=glx_set &part=pt_part &site=wo_site}

      if glx_mthd = "AVG"
         and reavg_yn
         and wo_qty_ord >= 0
      then glcost = newcst.
      else glcost = sct_cst_tot.
   end.
/*GUI*/ if global-beam-me-up then undo, leave.


   for each sr_wkfl exclusive-lock where sr_userid = mfguser
         and sr_lineid = cline:
/*GUI*/ if global-beam-me-up then undo, leave.

      {&WOWORCA-P-TAG3}
      gl_amt = sr_qty * glcost.

      find ld_det where ld_site = sr_site
         and ld_loc = sr_loc
         and ld_part = pt_part
         and ld_lot = sr_lotser
         and ld_ref = sr_ref
         no-lock no-error.
      if available ld_det then
         assign
            assay = ld_assay
            grade = ld_grade
            expire = ld_expire.

      /* RE-CALCULATE AVERAGE COST */
      if glx_mthd = "AVG" and reavg_yn then do:
         {gprun.i ""csavg03.p"" "(input recid(sct_det),
                                  input sr_qty,
                                  input newmtl_tl,
                                  input newlbr_tl,
                                  input newbdn_tl,
                                  input newovh_tl,
                                  input newsub_tl,
                                  input newmtl_ll,
                                  input newlbr_ll,
                                  input newbdn_ll,
                                  input newovh_ll,
                                  input newsub_ll)"
            }
/*GUI*/ if global-beam-me-up then undo, leave.

      end.

      /* CALL GPICLT.P TO CHECK FOR AND ADD THE LOTSERIAL TO THE LOT_MSTR */

      find first clc_ctrl no-lock no-error.
      if not available clc_ctrl then do:
         {gprun.i ""gpclccrt.p""}
/*GUI*/ if global-beam-me-up then undo, leave.

         find first clc_ctrl no-lock.
      end.
      if (clc_lotlevel <> 0) and (sr_lotser <> "") then do:
         {gprun.i ""gpiclt.p"" "(input wo_part,
                                 input sr_lotser,
                                 input wo_nbr,
                                 input wo_lot,
                                 output trans-ok )" }
/*GUI*/ if global-beam-me-up then undo, leave.

         if not trans-ok then do:
            {pxmsg.i &MSGNUM=2740 &ERRORLEVEL=4}
            /* CURRENT TRANSACTION REJECTED - */
            undo, next.        /* CONTINUE WITH NEXT TRANSACTION */
         end. /* IF NOT TRANS-OK */
      end. /* IF (CLC_LOTLEVEL <> 0) */

      /* CHANGED ARGUMENTS &lotnumber = wo_lot AND &ordernbr = wo_nbr */
      /* TO &lotnumber = l_wo_lot AND &ordernbr = l_wo_nbr */
      {ictrans.i
         &addrid=wo_vend
         &bdnstd=0
         &cracct=cr_acct
         &crsub=cr_sub
         &crcc=cr_cc
         &crproj="
            if wo_acct <> """" then
               wo_project
            else
               """""
         &curr=""""
         &dracct="
            if available pt_mstr then
               if available pld_det then pld_inv_acct
               else pl_inv_acct
            else """""
         &drsub="
            if available pt_mstr then
               if available pld_det then pld_inv_sub
               else pl_inv_sub
            else """""
         &drcc="
            if available pt_mstr then
               if available pld_det then pld_inv_cc
               else pl_inv_cc
            else """""
         &drproj="
            if wo_acct <> """" then
               wo_project
            else
               """""
         &effdate=eff_date
         &exrate=0
         &exrate2=0
         &exratetype=""""
         &exruseq=0
         &glamt=gl_amt
         &lbrstd=0
         &line=0
         &location="(if sr_site <> wo_site then pt_loc
                     else sr_loc)"
         &lotnumber=l_wo_lot
         &lotserial=sr_lotser
         &lotref=sr_ref
         &mtlstd=0
         &ordernbr=l_wo_nbr
         &ovhstd=0
         &part=wo_part
         &perfdate=?
         &price=glcost
         &quantityreq="wo_qty_ord - wo_qty_comp - wo_qty_rjct"
         &quantityshort=wo_bo_chg
         &quantity=sr_qty
         &revision=""""
         &rmks=rmks
         &shiptype=""""
         &site=wo_site
         &slspsn1=""""
         &slspsn2=""""
         &sojob=wo_so_job
         &substd=0
         &transtype=""RCT-WO""
         &msg=msgref
         &ref_site=tr_site
         }

      {&WOWORCA-P-TAG6}
      assign
         tr_fsm_type = wo_fsm_type
         tr_recno    = recid(tr_hist)
         tr_batch = wo_batch.

      /* UPDATE WORK ORDER */
      find wo_mstr exclusive-lock where recid(wo_mstr) = wo_recno.
      if max(wo_qty_ord - max(wo_qty_comp,0)
         - max(wo_qty_rjct,0),0) >= 0 then do:

         find in_mstr exclusive-lock where in_part = wo_part
            and in_site = wo_site no-error.

         if available in_mstr then do:

            if wo_qty_ord >= 0 then do:
               if wo_qty_ord - wo_qty_comp - wo_qty_rjct >= 0
                  /* ADJUST BY LESSER OF SR_QTY OR QTY OPEN */
               then
                  in_qty_ord = in_qty_ord - min(sr_qty,
                     max(wo_qty_ord - wo_qty_comp - wo_qty_rjct,0)).
               else
               if wo_qty_ord - wo_qty_comp - wo_qty_rjct - sr_qty  >= 0
                  /* ADJUST BY REVERSING NET QUANTITY            */
               then
                  in_qty_ord = in_qty_ord + wo_qty_ord - wo_qty_comp
                             - wo_qty_rjct - sr_qty.
            end.
            else do:
               if (wo_qty_ord - wo_qty_comp - wo_qty_rjct <= 0)
                  /* ADJUST BY GREATER OF SR_QTY OR QTY OPEN */
               then
                  in_qty_ord = in_qty_ord - max(sr_qty,
                     min(wo_qty_ord - wo_qty_comp - wo_qty_rjct,0)).
               else
               if wo_qty_ord - wo_qty_comp - wo_qty_rjct - sr_qty  <= 0
                  /* ADJUST BY REVERSING NET QUANTITY            */
               then
                  in_qty_ord = in_qty_ord + wo_qty_ord - wo_qty_comp
                             - wo_qty_rjct - sr_qty.
            end.
         end.
      end.

      for first gl_ctrl
         fields (gl_rnd_mthd) no-lock:
      end.
/*GUI*/ if global-beam-me-up then undo, leave.
 /* FOR FIRST gl_ctrl.. */

      /* ROUND PER BASE CURRENCY ROUND METHOD */
      run gpcrnd (input-output gl_amt, input gl_rnd_mthd).

      assign
         wo_qty_comp = wo_qty_comp + sr_qty
         wo_wip_tot = wo_wip_tot - gl_amt.
      if glx_mthd = "AVG" then trgl_type = "RCT-AVG".

      if sr_site <> wo_site then do:

         assign
            global_part = wo_part
            global_addr = wo_vend.

         {&WOWORCA-P-TAG7}
         /* CHANGED INPUT PARAMETER wo_lot AND wo_nbr TO l_wo_lot */
         /* AND l_wo_nbr RESPECTIVELY */
         {gprun.i ""icxfer.p"" "(input l_wo_lot,
                                 input sr_lotser,
                                 input sr_ref,
                                 input sr_ref,
                                 input sr_qty,
                                 input l_wo_nbr,
                                 input wo_so_job,
                                 input rmks,
                                 input wo_project,
                                 input eff_date,
                                 input wo_site,
                                 input pt_loc,
                                 input sr_site,
                                 input sr_loc,
                                 input no,
                                 input """",
                                 input ?,
                                 input """",
                                 input 0,
                                 output gl_cost,
                                 output iss_trnbr,
                                 output rct_trnbr,
                                 input-output assay,
                                 input-output grade,
                                 input-output expire)"
            }
/*GUI*/ if global-beam-me-up then undo, leave.

         {&WOWORCA-P-TAG8}

         find tr_hist no-lock where tr_trnbr = trmsg no-error.
      end.

      /*RUN THE WIP LOT TRACE EXIT PROGRAM.  NOTE "NO-ERROR".*/
      {gprun.i ""wlisrcx.p""
         "(tr_lot,
           0,
           tr_part,
           tr_site,
           tr_loc,
           tr_serial,
           tr_ref,
           sr_qty) NO-ERROR"}
/*GUI*/ if global-beam-me-up then undo, leave.

      if available tr_hist then do:
         {gprun.i ""worcat03.p"" "(input recid(sr_wkfl),
                                   input recid(tr_hist),
                                   input tr_recno,
                                   input wo_part,
                                   input eff_date,
                                   input-output chg_assay,
                                   input-output chg_grade,
                                   input-output chg_expire,
                                   input-output chg_status,
                                   input-output assay_actv,
                                   input-output grade_actv,
                                   input-output expire_actv,
                                   input-output status_actv)"}
/*GUI*/ if global-beam-me-up then undo, leave.


      end.

      if wo_qty_ord >= 0
      then
      open_ref = max(wo_qty_ord - max(wo_qty_comp,0) - max(wo_qty_rjct,0),0).
      else
      open_ref = min(wo_qty_ord - min(wo_qty_comp,0) - min(wo_qty_rjct,0),0).

      {mfmrw.i "wo_mstr" wo_part wo_nbr wo_lot """"
         wo_rel_date wo_due_date open_ref
         "SUPPLY" WORK_ORDER wo_site}

      {mfmrw.i "wo_scrap" wo_part wo_nbr wo_lot """"
         wo_rel_date wo_due_date
         "open_ref * (1 - wo_yield_pct / 100)"
         "DEMAND" SCRAP_REQUIREMENT wo_site}

      /* IF IT IS FLOW ORDER THEN CALCULATE THE VARIANCES LIKE 3.12 DOES */
      if wo_type = "w" then do:
         assign
            sct_recid   = recid(sct_det)
            pt_recid    = recid(pt_mstr)
            wip_entity  = sf_entity
            accum_wip   = accum_wip - gl_amt  /* GL_AMT FOR RCT-WO */
            qty_iss_rcv = sr_qty.

         if wo_acct <> "" then
            cr_proj = wo_project.

         {gprun.i ""icintr1a.p""}
/*GUI*/ if global-beam-me-up then undo, leave.

      end.

      if sr_lineid = "" then
         delete sr_wkfl.

   end.

   /*DEBIT WIP & CREDIT MFG APPLIED OVHD FOR OVERHEAD THIS LEVEL*/
   {gprun.i ""woovhd.p""}
/*GUI*/ if global-beam-me-up then undo, leave.


   /* DON'T CALCULATE VARIANCE FOR FLOW ORDER */
   if wo_type <> "w" then do:
      /* UPDATE VARIANCES AT RECEIPT */
      if glx_mthd <> "AVG" then do:
         wo_recid = recid(wo_mstr).
         transtype = "VAR-POST".
         {gprun.i ""wovarup.p""}
/*GUI*/ if global-beam-me-up then undo, leave.

      end.
   end. /* if wo_type <> "w" */
end.

if wo_rjct_chg <> 0 then do:
   /* ADDED FIRST INPUT PARAMETER pTrLot */
   {gprun.i ""woworcb.p"" "(input pTrLot)" }
/*GUI*/ if global-beam-me-up then undo, leave.

end.
{&WOWORCA-P-TAG4}
PROCEDURE update_wo_mstr:

   define input        parameter l_costset    like glx_set       no-undo.
   define input        parameter l_wo_part    like wo_part       no-undo.
   define input        parameter l_wo_site    like wo_site       no-undo.
   define input        parameter l_qty_chg    like tr_qty_loc    no-undo.
   define input        parameter l_method     like cs_method     no-undo.
   define input        parameter l_joint_type like wo_joint_type no-undo.
   define input-output parameter l_wo_mtl_upd like wo_mtl_totx   no-undo.
   define input-output parameter l_wo_lbr_upd like wo_lbr_totx   no-undo.
   define input-output parameter l_wo_bdn_upd like wo_bdn_totx   no-undo.
   define input-output parameter l_wo_sub_upd like wo_sub_totx   no-undo.

   for first sct_det
      fields (sct_bdn_ll sct_bdn_tl
              sct_lbr_ll sct_lbr_tl
              sct_mtl_ll sct_mtl_tl
              sct_sub_ll sct_sub_tl
              sct_ovh_ll sct_ovh_tl
              sct_part sct_sim sct_site)
      where sct_sim  = l_costset
        and sct_part = l_wo_part
        and sct_site = l_wo_site no-lock:

      /* MODIFIED THE CODE TO PREVENT THIS-LEVEL MATERIAL COST BEING */
      /* MOVED TO INVENTORY WHEN COSTING METHOD IS AVERAGE           */
      if l_method = "AVG"
      then do:

         l_wo_mtl_upd = l_wo_mtl_upd +
         ((sct_mtl_ll + sct_lbr_ll + sct_bdn_ll
         + sct_ovh_ll + sct_sub_ll) * l_qty_chg).

         /* BY-PRODUCT THIS-LEVEL MATERIAL COST MOVED TO INVENTORY */
         /* IN ORDER TO CORRECTLY ALLOCATE COST TO THE CO-PRODUCTS */
         if l_joint_type = "2"
         then
            l_wo_mtl_upd = l_wo_mtl_upd + (sct_mtl_tl * l_qty_chg).

      end.

      else
      if l_method = "STD"
      then
         l_wo_mtl_upd = l_wo_mtl_upd +
         ((sct_mtl_tl + sct_mtl_ll + sct_lbr_ll + sct_bdn_ll
         + sct_ovh_ll + sct_sub_ll) * l_qty_chg).

      assign
         l_wo_lbr_upd = l_wo_lbr_upd + (sct_lbr_tl * l_qty_chg)
         l_wo_bdn_upd = l_wo_bdn_upd + (sct_bdn_tl * l_qty_chg)
         l_wo_sub_upd = l_wo_sub_upd + (sct_sub_tl * l_qty_chg).

   end. /* FOR FIRST sct_det ... */

end. /* END PROCEDURE */
